package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.mapper.DataDictionaryDto;
import com.demo.mapper.DataDictionaryMapper;
import com.star.annotation.Dictionary;
import com.star.pojo.dto.DataDictionary;
import com.star.service.IDataDictionary;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典实现类
 *
 * @author: star.zhu
 * @date: 2020-07-30
 */
@Slf4j
@Service
public class DataDictionaryImpl implements IDataDictionary {

    @Autowired
    DataDictionaryMapper dataDictionaryMapper;

    @Override
    public List<DataDictionary> queryDictionary(Dictionary dictionary, String name) {
        QueryWrapper<DataDictionaryDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("dict_code", "dict_desc");
        if (null != dictionary) {
            if (0 != dictionary.no()) {
                queryWrapper.eq("dict_no", dictionary.no());
            } else if (StringUtils.hasText(dictionary.fieldName())) {
                queryWrapper.eq("keyword", dictionary.fieldName());
            }
        } else {
            queryWrapper.eq("keyword", name);
        }
        queryWrapper.eq("is_deleted", 0);
        List<DataDictionaryDto> dictionaryList = dataDictionaryMapper.selectList(queryWrapper);
        List<DataDictionary> list = new ArrayList<>(dictionaryList.size());
        for(DataDictionaryDto dto:dictionaryList){
            DataDictionary dataDictionary = dto;
            log.info(dataDictionary.getDictDesc());
            list.add(dataDictionary);
        }
        return list;
    }
}
